package com.inossem.warehousestorage_inossem.adapter.up_frame;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrameOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.UpFrame_ManageActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameManageBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by cjz on 2017/6/6.
 */

public class UpFrameListAdapter extends BaseAdapter {

    private Context mContext;
    private List<UpFrameManageBean.UpFrameListBean> mUpFrameList;
    private UpFrame_ManageActivity.UpFrameManageListener mUpFrameManageListener;
    private LayoutInflater inflater;
    private String mCondition;


    public UpFrameListAdapter(Context context, List<UpFrameManageBean.UpFrameListBean> upFrameList, String condition, UpFrame_ManageActivity.UpFrameManageListener upFrameManageListener) {
        mContext = context;
        mUpFrameList = upFrameList;
        mCondition = condition;
        mUpFrameManageListener = upFrameManageListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mUpFrameList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mUpFrameList.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_up_frame_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            condition.setText(mCondition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, condition);
                    mUpFrameManageListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mUpFrameManageListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            UpFrameManageBean.UpFrameListBean item = (UpFrameManageBean.UpFrameListBean) getItem(position);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_up_frame_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, item);

        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.inStorageNumber = (TextView) view.findViewById(R.id.inStorageNumber);
        viewHolder.createDate = (TextView) view.findViewById(R.id.createDate);
        viewHolder.warehouseName = (TextView) view.findViewById(R.id.warehouseName);
        viewHolder.inStorageType = (TextView) view.findViewById(R.id.inStorageType);
        viewHolder.inStorageNumberTextView = (TextView) view.findViewById(R.id.inStorageNumberTextView);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, UpFrameManageBean.UpFrameListBean item) {
        //暂时注释
//        viewHolder.inStorageNumberTextView.setText(item.getOrderName() + ":");
        viewHolder.inStorageNumber.setText(item.getRefer_receipt_code());
        viewHolder.createDate.setText(item.getCreate_time());
        viewHolder.warehouseName.setText(item.getWh_name());
        viewHolder.inStorageType.setText(item.getType_name());

        //事件处理
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, UpFrameOverviewActivity.class);
                intent.putExtra(UpFrameExtra.REQUEST_NUMBER, item.getStock_task_req_id());
                context.startActivity(intent);
            }
        });

    }

    private class ViewHolder {
        private TextView inStorageNumber, createDate, warehouseName, inStorageType, inStorageNumberTextView;
    }
}
